package com.zhisheng.day10_tlias.controller;

import com.zhisheng.day10_tlias.dto.EmpQueryDto;
import com.zhisheng.day10_tlias.entry.Emp;
import com.zhisheng.day10_tlias.entry.R;
import com.zhisheng.day10_tlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("emps")
public class EmpController {

    @Autowired
    private EmpService service;

    // 带分页带条件的查询
    @GetMapping
    public R findPageByCondition(EmpQueryDto dto){
        Map<String, Object> map = service.findPageByCondition(dto);
        return R.success(map);
    }

    @DeleteMapping("{ids}")
    public R<String> delByIds(@PathVariable("ids")List<Integer> ids){
        //int i1 = Integer.parseInt("abc");
        int i  = service.delByIds(ids);
        if(i >= 1){
            return R.success(null);
        }
        return R.fail("删除:"+ ids+"失败了!");
    }

    @PostMapping
    public R<String> insertEmp(@RequestBody Emp emp){
        int i  = service.insertEmp(emp);
        if(i >= 1){
            return R.success(null);
        }
        return R.fail("添加:"+emp+"失败了!");
    }

    @GetMapping("{id}")
    public R<Emp> findEmpById(@PathVariable("id")Integer id){
        Emp i  = service.findEmpById(id);
        if(i != null){
            return R.success(i);
        }
        return R.fail("根据:"+ id+"查询失败了!");
    }


    @PutMapping
    public R<String> updateEmp(@RequestBody Emp emp){
        int i  = service.updateEmp(emp);
        if(i >= 1){
            return R.success(null);
        }
        return R.fail("更新:"+emp+"失败了!");
    }

}
